package com.leyou.user.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.constants.LyConstants;
import com.leyou.common.constants.MQConstants;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    public Boolean checkData(String data, Integer type) {
        //封装条件
        User user = new User();
        //根据条件来判断数据类型
        switch (type) {
            case 1:
                user.setUsername(data);
                break;
            case 2:
                user.setPhone(data);
                break;
            default:
                throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        Wrapper<User> wrapper = Wrappers.query(user);
        //查询数据库，校验数据是否可用
        Integer count = userMapper.selectCount(wrapper);
        return count==0;
    }

    public void sendCode(String phone) {
        //生成6位随机验证码
        String code = RandomStringUtils.randomNumeric(6);
        //向redis中放一份
        redisTemplate.opsForValue().set(LyConstants.CODE_KEY_PRE+phone, code, 2, TimeUnit.DAYS);
        //封装一个发送给MQ的消息体
        Map<String, String> smsMap = new HashMap<>();
        smsMap.put("phone", phone);
        smsMap.put("code", code);
        //向mq中发送一份【发送给用户】
        amqpTemplate.convertAndSend(MQConstants.Exchange.SMS_EXCHANGE_NAME,
                MQConstants.RoutingKey.VERIFY_CODE_KEY, smsMap);
    }

    public void register(User user, String code) {
        //获取reids中的用户的验证码
        String redisCode = redisTemplate.opsForValue().get(LyConstants.CODE_KEY_PRE + user.getPhone());
        //判断用户输入的验证码是否正确
        boolean isTrue = StringUtils.equals(redisCode, code);
        if(!isTrue){
            throw new LyException(ExceptionEnum.INVALID_VERIFY_CODE);
        }
        //对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //数据入库
        int count = userMapper.insert(user);
        if(count==0){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public User findUser(String username, String password) {
        //封装用户名信息
        User entity = new User();
        entity.setUsername(username);
        QueryWrapper<User> wrapper = Wrappers.query(entity);
        //根据用户名查询用户对象
        User user = userMapper.selectOne(wrapper);
        if(user==null){
            throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        //判断密码是否正确
        if(!passwordEncoder.matches(password, user.getPassword())){
            throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        return user;
    }



}